package com.zjt.smart.ui.adapter.base;

import android.content.Context;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * 类似于ExpandableListActivity的Adapter
 */
public abstract class ExpandableAdapter<ParentHolderType extends BaseAdapter.BaseHolder,
        ChildHolderType extends BaseAdapter.BaseHolder> extends TypeAdapter{
    public static final int ITEM_TYPE_PARENT = 1;
    public static final int ITEM_TYPE_CHILD = 2;

    public ExpandableAdapter(Context mContext) {
        super(mContext);
    }

//    @Override
//    public void addAll(List<Object> newData) {
//        if (newData != null && newData.size() > 0
//                && !(newData.get(0) instanceof ExpandableAdapter.DataSet)) {
//            throw new IllegalArgumentException("The data should extends ExpandableAdapter.DataSet");
//        }
//        super.addAll(newData);
//    }

    @Override
    public int getItemType(int position) {
        Object obj = getOne(position);
        if (obj instanceof ExpandableAdapter.DataSet){
            return ITEM_TYPE_PARENT;
        }else {
            return ITEM_TYPE_CHILD;
        }
    }

    @Override
    protected List<Object> serializeList(List list) {
        if (list == null || list.size() == 0) return list;
        List<Object> objects = new ArrayList<>();
//        List<DataSet> setList = list;
//        for (DataSet set: setList) {
//            objects.add(set);
//
//            for (Object obj: set.childs) {
//                objects.add(obj);
//            }
//        }

        for (Object obj : list) {
            objects.add(obj);
        }
        return objects;
    }

    @Override
    protected void onBindItemDataToView(BaseHolder holder, Object itemData, int position) {
        int viewType = getItemType(position);
        switch (viewType){
            case ITEM_TYPE_PARENT:
                onBindParent((ParentHolderType)holder,((DataSet)itemData).parent,position);
                break;
            case ITEM_TYPE_CHILD:
                onBindChild((ChildHolderType) holder,itemData,position);
                break;
        }
    }

    @Override
    protected BaseHolder onCreateHolder(ViewGroup parent, int viewType) {
        switch (viewType){
            case ITEM_TYPE_PARENT:
                return onCreateParentHolder(parent,viewType);
            case ITEM_TYPE_CHILD:
                return onCreateChildHolder(parent,viewType);
        }
        return null;
    }

    protected abstract void onBindParent(ParentHolderType holder, Object parent, int position);

    protected abstract void onBindChild(ChildHolderType holder, Object child, int position);

    protected abstract ParentHolderType onCreateParentHolder(ViewGroup parent, int viewType);


    protected abstract ChildHolderType onCreateChildHolder(ViewGroup parent, int viewType);

    public class DataSet {
        public DataSet(Object parent, List<?> childs) {
            this.parent = parent;
            this.childs = childs;
        }
        Object parent;
        List<?> childs;
    }
}
